cc.Class({
    extends: cc.Component,

    properties: {
        range: cc.Sprite,
        main:cc.Sprite,
        numBol: false,
        Seconds: cc.Label,
        countdown:cc.Node,
    },

    // use this for initialization
    onLoad: function () {

    },

    init(img1, img2){
        if(img1 && img2){
            this.range.spriteFrame = img1;
            this.main.spriteFrame = img2;
        }
    },

    startTime(num){
        this.node.children[0].active=true;
        this.totalTime = num;
        this.restTime = num;
        this.main.fillRange = 1;
        this.isStart = true;
    },
    SecondsStart(num){
        this.node.children[1].active=true;
        this.oneSecond=0;
        this.Seconds.string=num+'s';
        this.totalTime = num;
        this.restTime = num;
        this.isStartSeconds = true;
    },
    endTime(){
        this.isStart = false;
        return this.restTime / this.totalTime
    },

    updateWidth(){
        var nowWidth = this.restTime / this.totalTime;
        this.main.fillRange = nowWidth;
        if(nowWidth <=0){
            this.isStart = false;
            cc.gameStart.game.processEndFun && cc.gameStart.game.processEndFun();
            var processEndTime = cc.gameStart.game.processEndTime ? cc.gameStart.game.processEndTime : 0 ;
            this.scheduleOnce(function() {
                cc.gameStart.EndAnimat();
            }, processEndTime);
        }
    },
    updateSeconds(){
        this.restTime -=1;
        this.Seconds.string = this.restTime+'s';
        //this.Seconds.node.getComponent(cc.Animation).play();
        if(this.restTime<=3){
            var fadeIn = cc.fadeIn(.1);
            var fadeOut = cc.fadeOut(.1);
            this.countdown.stopAllActions();
            this.countdown.runAction(cc.repeatForever(cc.sequence(fadeIn, fadeOut, fadeIn, fadeOut)));
            cc.voicePlay.playEffect("countdown", false);
        }
        if(this.restTime<=0){
            this.isStartSeconds=false;
            this.countdown.stopAllActions();
            this.countdown.opacity=0;
            cc.gameStart.EndAnimat(true);
        }
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if(this.isStart){
            this.restTime -= dt;
            this.updateWidth();
        }
        if(this.isStartSeconds){
            this.oneSecond+=dt
            if(this.oneSecond>=1){
                this.oneSecond=0;
                this.updateSeconds();
            }
        }
    },
});
